package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("AdminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "后台菜品管理")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @ApiOperation(value = "通过类别id获取菜品列表")
    @GetMapping("/list")
    public Result<List<Dish>> getDishList(Long categoryId,String name) {
        log.info("搜索查询菜品名称：{}", name);
        log.info("获取菜品列表，类别id：{}", categoryId);
        List<Dish> dishList = dishService.getDishList(categoryId,name);
        return Result.success(dishList);
    }

    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result addDishWithFlavors(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.addDishWithFlavors(dishDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> getDishPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品：{}", dishPageQueryDTO);
       PageResult pageResult= dishService.getDishPage(dishPageQueryDTO);
       return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("删除菜品：{}", ids);
        dishService.deleteDish(ids);
        return Result.success();
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据菜品id查询菜品信息")
    public Result<DishVO> getDishById(@PathVariable(value = "id", required = false) Long id){
        log.info("查询菜品：{}", id);
        DishVO dishvo = dishService.getDishById(id);
        return Result.success(dishvo);
    }
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateDishStatus(@PathVariable("status") Integer status, @RequestParam Integer id){
        log.info("修改菜品状态：{}", id);
        dishService.updateDishStatus(status, id);
        return Result.success();
    }
}
